package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author zhl
 * @version 1.0
 * @ClassName
 * @date 2020/11/2 17:52
 */
public class FileWrite {
    public static void main(String[] args) {
        try {
            System.setProperty("HADOOP_USER_NAME", "hadoop");
            String HDFSUri = "hdfs://192.168.136.132:9000";
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS", HDFSUri);
            FileSystem fs = FileSystem.get(new URI(HDFSUri), conf, "hadoop");
            byte[] buff = "Hello World\n".getBytes();
            String filename = "input/test1";
            FSDataOutputStream fsdOS = fs.create(new Path(filename));
            fsdOS.write(buff, 0, buff.length);
            System.out.println("Create:"+filename);
            fsdOS.close();
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
